package com.ate.ui;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.math.MathUtils;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.telephony.PhoneNumberUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.ate.MyApp;
import com.ate.adapters.BuyCarAdapter;
import com.ate.alipay.PayDemoActivity;
import com.ate.bean.BuyCarBean;
import com.ate.bean.GoodsBean;
import com.ate.gen.DaoManager;
import com.ate.gen.DaoSession;
import com.ate.javamvp.R;
import com.ate.lib_core.http.mvp.view.BaseActity;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.List;

public class BuyCarActivity extends BaseActity {

    private RecyclerView buycarRecy;
    private BuyCarAdapter buyCarAdapter;
    private Button pay;
    private CheckBox allCheck;
    private TextView sumPricetv;


    @Override
    public int bandlayout() {
        return R.layout.activity_buy_car;
    }

    @Override
    public void initView() {
        buycarRecy = (RecyclerView) findViewById(R.id.buycar_recy);

    }

    float num = 0;

    @Override
    public void initData() {
        DaoManager daoManager = DaoManager.getInstance();
        daoManager.init(this);
        DaoSession daoSession = daoManager.getDaoSession();
        List<BuyCarBean> dataBeans = daoSession.loadAll(BuyCarBean.class); 
        buyCarAdapter = new BuyCarAdapter(R.layout.buycar_item_layout, dataBeans);
        buycarRecy.setAdapter(buyCarAdapter);
        buycarRecy.setLayoutManager(new LinearLayoutManager(this));
        DividerItemDecoration decoration = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
        buycarRecy.addItemDecoration(decoration);

        pay = (Button) findViewById(R.id.pay);
        allCheck = (CheckBox) findViewById(R.id.allCheck);
        sumPricetv = (TextView) findViewById(R.id.sumPrice);


        for (BuyCarBean dataBean : dataBeans) {
            if (dataBean.getIsChecked()) {
                num += dataBean.getBuyCount() * Float.parseFloat(dataBean.getPrice());
            }
        }
        sumPricetv.setText("￥ " + String.format("%.2f", num));

        buyCarAdapter.setOnAumentListener(new BuyCarAdapter.OnAumentListener() {
            @Override
            public void onAument(float sumPrice) {
                sumPricetv.setText("￥ " + sumPrice);
                num = sumPrice;
            }
        });
        pay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(BuyCarActivity.this, PayDemoActivity.class);
                intent.putExtra("money", num);
                intent.putExtra("name", "一坨奥里给");
                startActivity(intent);
            }
        });
    }
}